
%% Setup
load_settings;
% source_name = [data_root 'clean_data/collapsed_v1.mat']; 
source_name = 'ITER_DATA';

overall_baseline_mat_location = [trained_root 'sr1_model.mat'];

load(overall_baseline_mat_location);

%% load iteratively trained weights
% load([iter_root '1411rects.mat']);
% model.w = svmWeightVector';

%%

overall_baseline.libsvmmodel = model;
overall_baseline.model = model.w';

data = getData(source_name);

%data = addBackgroundModel(data);

    % holdout cameras....
    % dataset       index       cam id
    % sr1           4           8307
    % sr2           6           1411
    % live          6           1411
    % iter          5           1411

%% Eval classifier
[evaluated_data, stats] = calcClassifierStats(data, overall_baseline, [3] ); % if last is blank, evaluate on test data for each camera. Else, evaluate on all of holdout camera

%%
% stats = cell(1, size(data,1));
% for ix = 1:size(data,1)
%     baseline_mat_location = [trained_root 'iter_' num2str(ix) '_model.mat'];
% 
%     load(baseline_mat_location);
% 
%     baseline.libsvmmodel = model;
%     baseline.model = model.w';
%     [evaluated_data, stat] = calcClassifierStats(data(ix,:), baseline, [] );
%     stats{ix} = stat;
%     
% end
%% ROC Curve
%[X,Y,T,AUC] = perfcurve([ones(size(stats.ppl_dv)); -ones(size(stats.bg_dv))], [stats.ppl_dv; stats.bg_dv], -1);

% plot(X,Y);
% xlabel('False positive rate');
% ylabel('True positive rate');
% title(['collapsed v2 w/ background model on its holdout cam; AUC = ', num2str(AUC)]);
% print('-djpeg', 'collapsed_v2_bgm_holdout_roc');
%imshow(stats.img_of_weight);

%% patch montage
% fn = strcat(image_root, sprintf('%08d',data(13,26).cam_id), '/', num2str(data(13,26).day), '_', num2str(data(13,26).hour, '%06d'), '.jpg');
% 
% holdout = data(13,:);
% no = 0;
% for ix = 1:26
%     no = no + size(holdout(ix).bg_rects,1);
% end
% 
% rel_ppl_pred = stats.ppl_predictions(624:666);
% rel_bg_pred = stats.bg_predictions(476:503);
% %%
% 
% patchMontage(fn, holdout(26).ppl_rects(rel_ppl_pred == 1, :));
% 
% %%
% patchMontage(fn, holdout(26).bg_rects(rel_bg_pred == -1, :));


%% overview cam 1147 in test/
holdout = data(3,:);
bg_pred_ind = 1;
ppl_pred_ind = 1;
for ix = 1:length(holdout)
    fn = strcat(image_root, sprintf('%08d',holdout(ix).cam_id), '/', num2str(holdout(ix).day), '_', num2str(holdout(ix).hour, '%06d'), '.jpg');
    img = imread(fn);
    imshow(img);
%     title(fn);
    for jx = 1:size(holdout(ix).ppl_rects,1)
        color = 'g';
        if stats.ppl_predictions(ppl_pred_ind) == 1;
            rectangle('Position', holdout(ix).ppl_rects(jx,:), 'EdgeColor', color, 'LineWidth', 3);
        end
        ppl_pred_ind = ppl_pred_ind+1;
    end
    for jx = 1:size(holdout(ix).bg_rects,1)
        if stats.bg_predictions(bg_pred_ind) == 1;
            color = 'm';
        rectangle('Position', holdout(ix).bg_rects(jx,:), 'EdgeColor', color, 'LineWidth', 3);
        end
        bg_pred_ind = bg_pred_ind+1;
    end
    hold off;
    print(['../test/' sprintf('%08d',holdout(ix).cam_id), '_', ...
                              num2str(holdout(ix).day), '_', ...
                              num2str(holdout(ix).hour, '%06d')], ...
                              '-djpeg');
    pause(1);
end


